<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/9/20 21:45
 */

namespace module\document\logic;

use module\document\enum\DocumentPageType;
use module\document\enum\DocumentTypeEnum;
use module\document\model\Document;
use mof\annotation\Inject;
use mof\Logic;
use mof\Searcher;
use think\db\exception\DataNotFoundException;
use think\Paginator;

class DocumentLogic extends Logic
{
    #[Inject(Document::class)]
    protected $model;

    public function paginate(Searcher $searcher, bool $simple = false): Paginator
    {
        return $searcher->model($this->model())
            ->field(['content'], true)
            ->order(['id' => 'asc'])
            ->paginate($simple);
    }

    /**
     * @param $pageName
     * @return Document
     * @throws DataNotFoundException
     */
    public function page($pageName): Document
    {
        if (!$pageType = DocumentPageType::tryFrom($pageName)) {
            throw new DataNotFoundException('页面不存在');
        }
        /** @var Document $model */
        $model = $this->model->where('type', DocumentTypeEnum::PAGE->value)
            ->where('sub_type', $pageType->value)
            ->where('status', 1)
            ->find();
        if (!$model) {
            throw new DataNotFoundException('数据不存在');
        }
        $model->scene('page');

        return $model;
    }
}